from flask import Flask, request, jsonify, make_response
from flask_cors import CORS
from login import login
import time
from register import register

app = Flask(__name__)
# 允许所有来源的跨域请求
CORS(app)

@app.route('/login', methods=['POST'])
def login_route():
    try:
        data = request.get_json()
        password = data.get('password')
        username = data.get('username')
        if username and password:
            print('用户名：', username)
            print('密码：', password)
            info = login(username, password)
            response = {
                'status': info[0],
                'message': info[1]
            }
            response = make_response(jsonify(response), 200)
            if info[0] == "success": response.set_cookie('username', username)
            # response.headers['Access-Control-Allow-Origin'] = '*'
            return response
        else:
            print(request)
            response = {
                'status': 'error',
                'message': '未提供有效的搜索信息'
            }
            return jsonify(response), 400
    except Exception as e:
        print(request)
        response = {
            'status': 'error',
            'message': str(e)
        }
        return jsonify(response), 500
    
@app.route('/search', methods=['POST'])
def search_route():
    try:
        data = request.get_json()
        message = data.get('message')
        if message:
            print('搜索关键词：', message)
            results = [
                {'title': '搜索结果1', 'url': 'http://example.com/result1'},
                {'title': '搜索结果2', 'url': 'http://example.com/result2'},
                {'title': '搜索结果3', 'url': 'http://example.com/result3'}
            ]
            response = {
                'status': 'success',
                'results': results
            }
            return jsonify(response), 200
        else:
            print(request)
            response = {
                'status': 'error',
                'message': '未提供有效的搜索信息'
            }
            return jsonify(response), 400
    except Exception as e:
        print(request)
        response = {
            'status': 'error',
            'message': str(e)
        }
        return jsonify(response), 500
    
@app.route('/register', methods=['POST'])
def register_route():
    try:
        data = request.get_json()
        username = data.get('username')
        password = data.get('password')
        state = register(username, password)
        if state[0] == 1:
            response = {
                'status': 'success',
                'message': '注册成功'
            }
            return jsonify(response), 200
        else:
            response = {
                'status': 'error',
                'message': '用户名已存在'
            }
            return jsonify(response), 200
    except Exception as e:
        print(request)
        response = {
            'status': 'error',
            'message': str(e)
        }
        return jsonify(response), 500

if __name__ == '__main__':
    app.run(host='localhost', port=8080, debug=True)